import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
//import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
 import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import API from './api/index'
import TMP from './utils/tools'
import store from './store'  //引入store
import axios from "axios";

Vue.config.productionTip = false;
Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

//跨域
const instance = axios.create({
    baseURL : 'http://localhost:9999/',
    timeout : 4000
})
// 添加响应拦截器
function responseInterceptor (response) {
    // 对响应数据做点什么
    if(response.data.code != 200) {
        //对提交的数据校验失败的全局处理
        if(response.data.code == 400) {
           /* let errors = response.data.data.errors;
            let emsg = "";
            for (let i = 0; i < errors.length; i++) {
                emsg += errors[i].msg;
                if(i < errors.length - 1) {
                    emsg += ";";
                }
            }
            alert("提交的数据不正确：" + emsg);*/
            alert(response.data.msg);
        } else {
            alert(response.data.msg);
        }
    }
    return response;
}
//给所以请求添加消息头
function resquestInterceptor(config){
    config.headers.common['token'] = window.localStorage.getItem("token");
    return config;
}
//登录失败的拦截
instance.interceptors.response.use(responseInterceptor);
instance.interceptors.request.use(resquestInterceptor);


Vue.prototype.$axios = instance;



// 时间戳转换  （全局定义写在main.js里面）
Vue.filter("dateFormate", function (originVal) {
    const dt = new Date(originVal);

    const y = dt.getFullYear();
    const m = (dt.getMonth() + 1 + "").padStart(2, "0");
    const d = (dt.getDate() + "").padStart(2, "0");

    const hh = (dt.getHours() + "").padStart(2, "0");
    const mm = (dt.getMinutes() + "").padStart(2, "0");
    const ss = (dt.getSeconds() + "").padStart(2, "0");

    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
});
new Vue({
    router,
    i18n,
    store,
    render: h => h(App)
}).$mount('#app');
